package com.dc.androidutilcode.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;

/**
 * 屏幕工具类
 */
public class ScreenUtils {

    /**
     * 获取屏幕的宽度（单位：px）
     *
     * @param context Context
     * @return 屏幕的宽度
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (wm == null) {
            return -1;
        }
        Point point = new Point();
        wm.getDefaultDisplay().getRealSize(point);
        return point.x;
    }

    /**
     * 获取屏幕的高度（单位：px）
     *
     * @param context Context
     * @return 屏幕的高度
     */
    public static int getScreenHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (wm == null) {
            return -1;
        }
        Point point = new Point();
        wm.getDefaultDisplay().getRealSize(point);
        return point.y;
    }

    /**
     * 获取屏幕密度
     *
     * @return 屏幕密度
     */
    public static float getScreenDensity() {
        return Resources.getSystem().getDisplayMetrics().density;
    }

    /**
     * 获取屏幕密度 DPI
     *
     * @return 屏幕密度 DPI
     */
    public static int getScreenDensityDpi() {
        return Resources.getSystem().getDisplayMetrics().densityDpi;
    }

    /**
     * 获取view上边界到父View上边距的距离
     *
     * @param view View
     * @return view上边界到父View上边距的距离
     */
    public static int getTop(View view) {
        return view.getTop();
    }

    /**
     * 获取view左边界到父View左边距的距离
     *
     * @param view View
     * @return view左边界到父View左边距的距离
     */
    public static int getLeft(View view) {
        return view.getLeft();
    }

    /**
     * 获取view下边界到父View上边距的距离
     *
     * @param view View
     * @return view下边界到父View上边距的距离
     */
    public static int getBottom(View view) {
        return view.getBottom();
    }

    /**
     * 获取view右边界到父View左边距的距离
     *
     * @param view View
     * @return view右边界到父View左边距的距离
     */
    public static int getRight(View view) {
        return view.getRight();
    }

    /**
     * 获取view在其父窗口的横坐标（获得View相对其父窗口的绝对坐标）
     *
     * @param view View
     * @return view在屏幕中的横坐标
     */
    public static int getViewWindowX(View view) {
        int[] point = new int[2];
        view.getLocationInWindow(point);
        return point[0];
    }

    /**
     * 获取view在其父窗口的纵坐标（获得View相对其父窗口的绝对坐标）
     *
     * @param view View
     * @return view在屏幕中的纵坐标
     */
    public static int getViewWindowY(View view) {
        int[] point = new int[2];
        view.getLocationInWindow(point);
        return point[1];
    }

    /**
     * 获取view在屏幕中的横坐标（获得View相对屏幕的绝对坐标）
     *
     * @param view View
     * @return view在屏幕中的横坐标
     */
    public static int getViewScreenX(View view) {
        int[] point = new int[2];
        view.getLocationOnScreen(point);
        return point[0];
    }

    /**
     * 获取view在屏幕中的纵坐标（获得View相对屏幕的绝对坐标）
     *
     * @param view View
     * @return view在屏幕中的纵坐标
     */
    public static int getViewScreenY(View view) {
        int[] point = new int[2];
        view.getLocationOnScreen(point);
        return point[1];
    }

    /**
     * 设置屏幕为全屏
     *
     * @param activity Activity
     */
    public static void setFullScreen(@NonNull final Activity activity) {
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    /**
     * 设置屏幕为非全屏
     *
     * @param activity Activity
     */
    public static void setNonFullScreen(@NonNull final Activity activity) {
        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    /**
     * 判断屏幕是否为全屏
     *
     * @param activity Activity
     * @return 是否为全屏
     */
    public static boolean isFullScreen(@NonNull final Activity activity) {
        int fullScreenFlag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        return (activity.getWindow().getAttributes().flags & fullScreenFlag) == fullScreenFlag;
    }

    /**
     * Toggle full screen.
     *
     * @param activity The activity.
     */
    public static void toggleFullScreen(@NonNull final Activity activity) {
        boolean isFullScreen = isFullScreen(activity);
        Window window = activity.getWindow();
        if (isFullScreen) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }

    /**
     * 设置屏幕为横屏
     *
     * @param activity Activity
     */
    public static void setLandscape(@NonNull final Activity activity) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    /**
     * 设置屏幕为竖屏
     *
     * @param activity Activity
     */
    public static void setPortrait(@NonNull final Activity activity) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    /**
     * 判断是否横屏
     *
     * @param context Context
     * @return 是否横屏
     */
    public static boolean isLandscape(Context context) {
        return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
    }

    /**
     * 判断是否竖屏
     *
     * @param context Context
     * @return 是否竖屏
     */
    public static boolean isPortrait(Context context) {
        return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    }
}
